//
//  BoxVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/4/12.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "BoxVC.h"
#import "BoxCollectionViewCell.h"
#import "IOTVC.h"
#import "MovingAnnotationViewController.h"
#import "HomeWorkTimeVC.h"
#import "RepairVC.h"
#import "OilLevelVC.h"
@interface BoxVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *mainCollectionView;
@property (strong, nonatomic) NSMutableArray *arrImageName;//collect数据源
@property (strong, nonatomic) NSMutableArray *arrTitle;//标签名
@property (assign, nonatomic) NSInteger headerViewHieght;
@property (strong, nonatomic) UIAlertView *alertView;
@property (strong, nonatomic) NSDictionary *dicResult;

@end

@implementation BoxVC
static NSString *const collIdentifier = @"BoxCollectionViewCell";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"小马云盒";
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
    [_mainCollectionView registerNib:[UINib nibWithNibName:@"BoxCollectionViewCell" bundle:nil]  forCellWithReuseIdentifier:collIdentifier];

    self.arrTitle = [NSMutableArray arrayWithObjects:@"工作小时",@"油位",@"数据报表",@"维修",@"现场视频",@"", nil];
    self.arrImageName = [NSMutableArray
                         arrayWithObjects:@"TIME",@"油位",@"数据报表-1",@"维修-1",@"视频-1",@"", nil];
}

#pragma mark ---- UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.arrTitle.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    BoxCollectionViewCell *cell = (BoxCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:collIdentifier forIndexPath:indexPath];


        cell.imgView.image = [UIImage imageNamed:self.arrImageName[indexPath.row]];
        cell.titleLabel.text = self.arrTitle[indexPath.row];
    return cell;
}

// 和UITableView类似，UICollectionView也可设置段头段尾
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
//        _mainCollectHeaderView = [_mainCollectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
//        if(_mainCollectHeaderView == nil){
//            _mainCollectHeaderView = [[HomeHeadCollectionReusableView alloc] init];
//        }
//        _mainCollectHeaderView.backgroundColor = [UIColor whiteColor];
//        _mainCollectHeaderView.imageAngel.hidden = YES;
//        _mainCollectHeaderView.imageConnect.hidden = YES;
//        _mainCollectHeaderView.imageActivity.hidden = YES;
//        _mainCollectHeaderView.labelRanking.hidden = YES;
//        [_mainCollectHeaderView.btnRanking addTarget:self action:@selector(ranking) forControlEvents:UIControlEventTouchUpInside];
//        [_mainCollectHeaderView.btnActivity addTarget:self action:@selector(activity) forControlEvents:UIControlEventTouchUpInside];
//        [_mainCollectHeaderView.btnConnect addTarget:self action:@selector(connectBEL) forControlEvents:UIControlEventTouchUpInside];
//        [_mainCollectHeaderView.btnAngel addTarget:self action:@selector(housekeeper) forControlEvents:UIControlEventTouchUpInside];
//        return _mainCollectHeaderView;
//    }else if([kind isEqualToString:UICollectionElementKindSectionFooter]){
//        _mainCollectFooterView = [_mainCollectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
//        if(_mainCollectFooterView == nil){
//            _mainCollectFooterView = [[HomeFooterCollectionReusableView alloc] init];
//        }
//        [_mainCollectFooterView.btnResetvation addTarget:self action:@selector(toGoodMedicalNetwork) forControlEvents:UIControlEventTouchUpInside];
//        [_mainCollectFooterView.btnPulse addTarget:self action:@selector(toDoctorPulse) forControlEvents:UIControlEventTouchUpInside];
//        return _mainCollectFooterView;
//    }
//    return nil;
//}

#pragma mark ---- UICollectionViewDelegateFlowLayout
//item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){floorf(kScreenWidth/3-2),floorf(kScreenWidth/3-2)};
}

//四周边间距
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (kScreenWidth == 320) {
        return UIEdgeInsetsMake(0,0.5, 0,0.5);
    }else{
        
        return UIEdgeInsetsMake(0,-1, 0,0);
    }
}

//行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (kScreenWidth == 320) {
        return 0.5f;
    }else{
        return 2.0f;
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    // return 0.0f;
    return 1.0f;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return (CGSize){kScreenWidth,self.headerViewHieght};
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (kScreenWidth == 320) {
        return (CGSize){kScreenWidth,54};
    }else{
        return (CGSize){kScreenWidth,60};
    }
}

#pragma mark ---- UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    BoxCollectionViewCell *cell = (BoxCollectionViewCell *)[_mainCollectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    BoxCollectionViewCell *cell = (BoxCollectionViewCell *)[_mainCollectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}

// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    NSString *strTitle = self.arrTitle[indexPath.row];
    
//    if ([strTitle isEqualToString:@"维修"]) {
//
//    RepairVC *vc = [[RepairVC alloc]init];
//
//    [self.navigationController pushViewController:vc animated:YES];
//
//        return;
//    }
    
    if ([strTitle isEqualToString:@"工作小时"]) {
        
        HomeWorkTimeVC *vc = [[HomeWorkTimeVC alloc]init];
      
        [self.navigationController pushViewController:vc animated:YES];
       
        return;
//        MovingAnnotationViewController *move = [[MovingAnnotationViewController alloc]init];
//        [self.navigationController pushViewController:move animated:YES];
    }
    if ([strTitle isEqualToString:@"油位"]) {
        OilLevelVC *vc = [[OilLevelVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    else{
        
        [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:@"该功能正在开发" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
    }
    
    
//    if ([strTitle isEqualToString:@"数据报表"]) {
//        HomeWorkTimeVC *vc = [[HomeWorkTimeVC alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
//
//        HealthViewController *health = [[UIStoryboard storyboardWithName:@"HealthViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"HealthViewController"];
//        [self.navigationController pushViewController:health animated:YES];
//    }else if ([strTitle isEqualToString:@"健康报告"]) {
//        if (![self logInspection]) {
//            return;
//        }
//        HealthReportViewController *healthReport = [[UIStoryboard storyboardWithName:@"HealthReportViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"HealthReportViewController"];
//        [self.navigationController pushViewController:healthReport animated:YES];
//    }else if ([strTitle isEqualToString:@"商城"]) {
//
//
//        MallViewController *mall = [[UIStoryboard storyboardWithName:@"MallViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"MallViewController"];
//        NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:UserDataID];
//        if ([str isEqualToString:@""] || str == nil || str == NULL) {
//            mall.youke = YES;
//        }
//        [self.navigationController pushViewController:mall animated:YES];
//    }else if ([strTitle isEqualToString:@"三级预警"]) {
//        if (![self logInspection]) {
//            return;
//        }
//        MessageViewController *message = getStoryOfControllerInstance(@"MessageViewController");
//        [self.navigationController pushViewController:message animated:YES];
//    }else if ([strTitle isEqualToString:@"自我挑战"]) {
//        ChallengeYourselfViewController *challengeYourself = [[UIStoryboard storyboardWithName:@"ChallengeYourselfViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ChallengeYourselfViewController"];
//        [self.navigationController pushViewController:challengeYourself animated:YES];
//    }else if ([strTitle isEqualToString:@"用户帮助"]) {
//
//        UserHelpViewController *user = [[UserHelpViewController alloc]init];
//        [self.navigationController pushViewController:user animated:YES];
//
//    }else if ([strTitle isEqualToString:@"文章订阅"]) {
//        ArticleViewController *ArticleVC = [[UIStoryboard storyboardWithName:@"ArticleViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"ArticleViewController"];
//        [self.navigationController pushViewController:ArticleVC animated:YES];
//    }else if ([strTitle isEqualToString:@""]) {
//
//    }else if ([strTitle isEqualToString:@"第九个itme"]){
//
//
//    }
//    HomeCollectionViewCell *cell = (HomeCollectionViewCell *)[_mainCollectionView cellForItemAtIndexPath:indexPath];
//    cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
//    [UIView animateWithDuration:0.3 animations:^{
//        cell.backgroundColor = [UIColor whiteColor];
//    } completion:^(BOOL finished) {
//
//    }];
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    BoxCollectionViewCell *cell = (BoxCollectionViewCell *)[_mainCollectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
}

@end
